//
//  CustomAnnotationMapViewController.m
//  UILbrary
//
//  Created by mac on 17/3/7.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "CustomAnnotationMapViewController.h"
#import "MKCustomAnnotation.h"
#import "MKCustomAnnotationView.h"
#import "MKcustomPinAnnotationView.h"
#import "CustomPinAnnotation.h"
#import "MKAnnotoationExampleCalloutView.h"
@import MapKit;

#define Latitude     22.6644412
#define Longitude    114.2155652

#define Latitude1     22.6644551
#define Longitude1    114.2155345

#define Latitude2     22.7844123
#define Longitude2    114.3455123
@interface CustomAnnotationMapViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) MKCustomAnnotation *customAnnotation;
@property (nonatomic, strong) CustomPinAnnotation *pinCustomAnnotation;

@end

@implementation CustomAnnotationMapViewController

#pragma mark - init and dealloc
- (instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)dealloc{
    self.mapView.delegate = nil;
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mapView];
    [self configMapView];
}

- (void)viewDidLayoutSubviews{
    self.mapView.frame = self.view.bounds;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - private methods
- (void)configMapView{
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(Latitude, Longitude);//MKCoordinateSpanMake(Latitude, Longitude);
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    [self.mapView setRegion:MKCoordinateRegionMake(center, span) animated:YES];
    [self.mapView addAnnotation:self.customAnnotation];
//    [self.mapView addAnnotation:self.pinCustomAnnotation];

    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(Latitude2, Longitude2);
    pointAnnotation.title = @"程文轩";
    pointAnnotation.subtitle = @"2017-03-08";
//    [self.mapView addAnnotation:pointAnnotation];
}

#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    if ([annotation isKindOfClass:[MKCustomAnnotation class]]) {
        MKCustomAnnotationView *customView = [[MKCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKCustomAnnotationView"];
        customView.image = [UIImage imageNamed:@"iconfont-baoji"];
        return customView;
    }
    if ([annotation isKindOfClass:[CustomPinAnnotation class]]) {
        MKcustomPinAnnotationView *view = [[MKcustomPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKcustomPinAnnotationView"];
        view.image = [UIImage imageNamed:@"pinLocation"];
        return view;
    }
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        MKAnnotoationExampleCalloutView *view = (MKAnnotoationExampleCalloutView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKAnnotoationExampleCalloutView"];
        
        if (!view) {
            view = [[MKAnnotoationExampleCalloutView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKAnnotoationExampleCalloutView"];
        }

        view.image = [UIImage imageNamed:@"pinLocation"];
        return view;
    }
    return nil;
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}
#pragma mark - lazy methods
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
        _mapView.showsScale = YES;
        _mapView.showsCompass = YES;
        _mapView.mapType = MKMapTypeStandard;
        _mapView.zoomEnabled = YES;
        _mapView.rotateEnabled = NO;
        _mapView.delegate = self;
    }
    return _mapView;
}

- (MKCustomAnnotation *)customAnnotation{
    if (!_customAnnotation) {
        _customAnnotation = [[MKCustomAnnotation alloc] init];
        _customAnnotation.deviceName = @"陈文轩";
        _customAnnotation.deviceStatue = @"250/KM";
        _customAnnotation.deviceSignalTime = @"2017-03-07 16:30:30";
        _customAnnotation.deviceLocationTime = @"2017-03-07 16:35:30";
        _customAnnotation.deviceBatteryLevel = @"良好";
        _customAnnotation.deviceAddress = @"广东省深圳市龙岗区横岗数字硅谷";
        _customAnnotation.coordinate = CLLocationCoordinate2DMake(Latitude, Longitude);
    }
    return _customAnnotation;
}

- (CustomPinAnnotation *)pinCustomAnnotation{
    if (!_pinCustomAnnotation) {
        _pinCustomAnnotation = [[CustomPinAnnotation alloc] init];
        _pinCustomAnnotation.coordinate = CLLocationCoordinate2DMake(Latitude1, Longitude1);
        _pinCustomAnnotation.title = @"地址";
        _pinCustomAnnotation.subtitle = @"时间";
    }
    return _pinCustomAnnotation;
}
@end
